<?php
// +----------------------------------------------------------------------------
// | Tke团队版权所属 [三十年河东三十年河西,莫欺少年穷.!]
// +----------------------------------------------------------------------------
// | Copyright (c) 2014 http://www.itdaodan.com All rights reserved.
// +----------------------------------------------------------------------------
// | Author: nengwan_03@163.com
// +----------------------------------------------------------------------------
/**
 +------------------------------------------------------------------------------
 * 系统设置
 +------------------------------------------------------------------------------
 * @version   1.0
 * @since 2014-8-13
 +------------------------------------------------------------------------------
 */
namespace Admin\Controller;
class ConfigController extends AdminController{
	
	/**
	 * @todo	配置管理
	 */
	public function index(){

		$where = array();
		if(isset($_GET['group'])){
			$where['group'] = (int)I('group');
		}
		if(isset($_GET['name'])){
			$where['name'] = array('like','%'.(string)I('name').'%');
		}
		// 记录当前列表页的cookie
		Cookie('__forward__',$_SERVER['REQUEST_URI']);
		
		$where['status'] = 1;
		$list = $this->lists('Config', $where,'sort asc,id asc');

		$this->assign('group',C('CONFIG_GROUP_LIST'));
		$this->assign('group_id',(int)I('group'));
		$this->assign('_list', $list);
		$this->assign('meta_title','配置管理');
		$this->display('index');
	}
	
	/**
	 * @todo	获取某个标签的配置参数
	 */
	public function group(){
		$id     =   (int)I('get.id',1);
		$type   =   C('CONFIG_GROUP_LIST');
		$list   =   M("Config")->where(array('status'=>1,'group'=>$id))->field('id,name,title,extra,value,remark,type')->order('sort')->select();
		if($list) {
			$this->assign('list',$list);
		}
		$this->assign('id',$id);		
		$this->assign('meta_title',$type[$id].'设置');
		$this->display('group');
	}
	
	/**
	 * @todo	新增配置
	 */
	public function add(){
		if(IS_POST){
			$obj = D('Config');
			$data = $obj->create();
			if($data){
				if($obj->add()){
					S('DB_CONFIG_DATA',null);
					$this->success('更新成功',Cookie('__forward__'));
				} else {
					$this->error('新增失败');
				}
			} else {
				$this->error($obj->getError());
			}
		}else{
			$this->assign('meta_title','配置管理');
			$this->assign('meta_title_c','新增配置');
			$this->display('add');
		}
	}
	
	/**
	 * @todo	编辑配置
	 */
	public function edit($id = 0){
		if(IS_POST){
			$obj = D('Config');
			$result = $obj->create();
			if($result){
                if($obj->save()){
                    S('DB_CONFIG_DATA',null);
                    //记录行为
                    //action_log('update_config','config',$obj['id'],UID);
                    $this->success('更新成功', Cookie('__forward__'));
                } else {
                    $this->error('更新失败');
                }
            } else {
                $this->error($obj->getError());
            }
		}else{
			$info = array();
			/* 获取数据 */
			$info = M('Config')->field(true)->find($id);
			
			if(false === $info){
				$this->error('获取配置信息错误');
			}
			$this->assign('info', $info);

			$this->assign('meta_title','配置管理');
			$this->assign('meta_title_c','编辑配置');
			$this->display('edit');
		}		
	}
	
	/**
	 * @todo	编辑配置
	 * @author  能万 <nengwan_03@163.com>
	 */
	public function del(){
		
		$id = array_unique((array)I('id',0));
		if(empty($id)){
			$this->error('选择要操作的数据!');
		}
		
		$where = array('id' => array(in,$id));
		if(M('Config')->where($where)->delete()){
			//S('DB_CONFIG_DATA',null);
			//记录行为
			//action_log('update_config','config',$id,UID);
			$this->success('删除成功');
		}else{
			$this->error('删除失败！');
		}
	}
	
	/**
	 * 配置排序
	 * @author 能万 <nengwan_03@163.com>
	 */
	public function sort(){
		if(IS_GET){
			$ids = I('get.ids');
	
			//获取排序的数据
			$map = array('status'=>array('gt',-1));
			if(!empty($ids)){
				$map['id'] = array('in',$ids);
			}elseif(I('group')){
				$map['group']	=	I('group');
			}
			$list = M('Config')->where($map)->field('id,title')->order('sort asc,id asc')->select();
	
			$this->assign('list', $list);

			$this->assign('meta_title','配置管理');
			$this->assign('meta_title_c','配置排序');
			$this->display('sort');
		}elseif (IS_POST){
			$ids = I('post.ids');
			$ids = explode(',', $ids);
			foreach ($ids as $key=>$value){
				$res = M('Config')->where(array('id'=>$value))->setField('sort', $key+1);
			}
			if($res !== false){
				$this->success('排序成功！',U('Config/index'));
			}else{
				$this->error('排序失败！');
			}
		}else{
			$this->error('非法请求！');
		}
	}
	
	/**
	 * 批量保存配置
	 */
	public function save($config){
		if($config && is_array($config)){
			$Config = M('Config');
			foreach ($config as $name => $value) {
				$map = array('name' => $name);
				$Config->where($map)->setField('value', $value);
			}
		}
		S('DB_CONFIG_DATA',null);
		$this->success('保存成功！');
	}
	
	
}